package com.lm.ip2region.controller;

import com.lm.ip2region.util.Ip2RegionUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("ip")
public class Ip2regionController {

    @GetMapping("/region")
    public ResponseEntity<String> getRegion(@RequestParam String ip) {
        try {
            String region = Ip2RegionUtil.getRegion(ip,false);
            return ResponseEntity.ok(region);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed to parse IP region");
        }
    }
}
